import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import List from '../components/goods/List.vue'
import ADD_GOOD from '../components/goods/Add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../components/Login.vue')
  },
  {
    path: "/home", redirect: 'welcome', component: Home,
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users,meta:{title:"用户列表"} },
      { path: '/rights', component: Rights ,meta:{title:"权限列表"}},
      { path: '/roles', component: Roles,meta:{title:"角色列表"} },
      { path: '/categories', component: Cate,meta:{title:"商品分类列表"} },
      { path: '/params', component: Params,meta:{title:"分类参数"} },
      { path: '/goods', component: List,meta:{title:"商品列表"}},
      { path: '/goods/add', component: ADD_GOOD,meta:{title:"添加商品"} },
      { path: '/orders', component: Order,meta:{title:"订单列表"} },
      { path: '/reports', component: Report,meta:{title:"报表"} },
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //to 要访问的路径
  //from 代表从哪个路径跳转而来
  // next 是一个函数 表示放行
  if (to.path === '/login') {
    return next()
  }
  if (!window.sessionStorage.getItem("token")) {
    return next("/login")
  }
  next()
})

export default router
